<?php
/**
 * Created by PhpStorm.
 * User: x
 * Date: 17-2-8
 * Time: 下午2:11
 */

namespace Fanpaiji;

use GuzzleHttp\Client;

class Http
{
    private static function getHttpClient($url)
    {
        return new Client([
            'cookies' => true,
            'headers' => [
                'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
                'Referer' => $url,
                'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
            ]
        ]);
    }

    public static function get($url)
    {

        $rt = self::getHttpClient($url)->request('GET',$url);
        $html = (string)$rt->getBody();
        return $html;

    }

    public static function _post($url,$data)
    {
        $rt = self::getHttpClient($url)->request('POST', $url, [
            'query' => $data
        ]);

//        $body = $response->getBody();
//// Implicitly cast the body to a string and echo it
//        echo $body;
//// Explicitly cast the body to a string
//        $stringBody = (string) $body;
//// Read 10 bytes from the body
//        $tenBytes = $body->read(10);
//// Read the remaining contents of the body as a string
//        $remainingBytes = $body->getContents();

        $html = (string)$rt->getBody();
        return $html;
    }
}